package com.kma.ncpractice2013.facade;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;

/**
  * Author: Katya Bileka
 * Date: 14.10.13
 * Time: 18:05
 */
public class MailFacadeImpl implements MailFacade {
    @Override
    public void sendLetter(String to, String subject, String textOfLetter) throws Exception {
        InitialContext ctx = new InitialContext();
        Session session = (Session) ctx.lookup("jndi/mail");

        Message msg = new MimeMessage(session);
        InternetAddress from = new InternetAddress(session.getProperty("mail.from"));
        msg.setSubject(subject);
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setFrom(from);
        msg.setText(textOfLetter);
        Transport.send(msg);
    }
}
